#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include"Init.hpp"
// std::string opers="+-*/%";

// enum{
//     DivZero=1,
//     ModZero,
//     Unknown
// };
using std::cout;
using std::endl;
using std::string;
Init init;
class Task
{
public:
    Task(int fd, const string &ip, uint16_t port)
        : _socket(fd),
          _ip(ip),
          _port(port)
    {}
    void run(string id)
    {
        char buff[1024];
        //Tcp 是面向字节流的,你怎么保证,你读取上来的数据是一个"完整"的报文呢? ?? 
        ssize_t n = read(_socket, buff, sizeof(buff));
        if (n > 0)
        {
            buff[n] = '\0';
            cout << "下面为你翻译\n";
            string echo_string = init(buff);
           n =  write(_socket, echo_string.c_str(), echo_string.size());
           if(n < 0)
           {
            cout << "write error!" << endl;
           }
        }
        else if (n == 0)
        {
            cout << "quit !" << endl;
        }
        else
        {
            cout << "error" << endl;
            exit(3);
        }
        close(_socket);
    }
    void operator()(string id)
    {
        run(id);
    }
    ~Task()
    {}

private:
    int _socket;
    std::string _ip;
    uint16_t _port;
};